package com.jc.algorithmes.binarytree;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 二叉树的层序遍历：
 * 1.先遍历第一层节点，再遍历第二层节点，依次类推
 * 2.采用迭代法实现
 */

public class LevelOrderDemoV2 {

    public static void main(String[] args) {
        TreeNode node7 = new TreeNode(7,null,null);
        TreeNode node6 = new TreeNode(6,null,null);
        TreeNode node5 = new TreeNode(5,node6,node7);
        TreeNode node4 = new TreeNode(4,null,null);
        TreeNode node3 = new TreeNode(3,null,null);
        TreeNode node2 = new TreeNode(2,node4,node5);
        TreeNode node1 = new TreeNode(1,node2,node3);

        levelOrder(node1);
    }

    public static void levelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (! queue.isEmpty()){
            TreeNode node = queue.poll();
            if (node != null){
                System.out.println(node.val);
                queue.add(node.left);
                queue.add(node.right);
            }
        }

    }
}
